package org.dht.com.tra.nw;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.log4j.Logger;
import org.dht.com.tra.CommunicationManagerWaitingResult;
import org.dht.com.tra.Communicator;


/**
 * The <code>CommunicationManagerNetworkLAN</code> abstract class management
 * send message multicast and recieves all messages from all sources. Required
 * params: BUFFER_SIZE_MULTICAST, IP_MULTICAST and PORT_MULTICAST
 * 
 * 
 */
public abstract class CommunicationManagerNetworkLAN extends
		CommunicationManagerWaitingResult {

	/**
	 * Properties for configuration CommunicationManagerNetworkLAN
	 * 
	 */
	public enum CommunicationManagerNetworkLANProperties {
		BUFFER_SIZE_MULTICAST, IP_MULTICAST, PORT_MULTICAST
	}

	/**
	 * Logger
	 */
	private static final Logger logger = Logger
			.getLogger(CommunicationManagerNetworkLAN.class);

	/**
	 * Reciever all messages
	 */
	private MessagesReciever messagesReciever;

	/*
	 * (non-Javadoc)
	 */
	public void init() {
		super.init();
		this.messagesReciever = new MessagesReciever(multicastManager,
				unicastManager, unicastBigManager, this);
		this.observableCommunication = messagesReciever;
	}

	/**
	 * Creates a Communicator for multicast manager. Are required of params in
	 * communication properties: PORT_MULTICAST, IP_MULTICAST and
	 * BUFFER_SIZE_MULTICAST
	 */
	protected Communicator createMulticastManager() {

		int portMulticast;
		String ipMulticast;
		long bufferSize;

		if (communicationProperties
				.containsKey(CommunicationManagerNetworkLANProperties.PORT_MULTICAST
						.name())) {
			portMulticast = Integer
					.parseInt(communicationProperties
							.get(CommunicationManagerNetworkLANProperties.PORT_MULTICAST
									.name()));
		} else {
			IllegalArgumentException illegalArgumentException = new IllegalArgumentException(
					"Property PORT_MULTICAST not found");

			logger.error("Property PORT_MULTICAST no found",
					illegalArgumentException);

			throw illegalArgumentException;
		}

		if (communicationProperties
				.containsKey(CommunicationManagerNetworkLANProperties.IP_MULTICAST
						.name())) {
			ipMulticast = communicationProperties
					.get(CommunicationManagerNetworkLANProperties.IP_MULTICAST
							.name());
		} else {
			IllegalArgumentException illegalArgumentException = new IllegalArgumentException(
					"Property IP_MULTICAST not found");

			logger.error("Property IP_MULTICAST no found",
					illegalArgumentException);

			throw illegalArgumentException;
		}

		if (communicationProperties
				.containsKey(CommunicationManagerNetworkLANProperties.BUFFER_SIZE_MULTICAST
						.name())) {
			bufferSize = Long
					.parseLong(communicationProperties
							.get(CommunicationManagerNetworkLANProperties.BUFFER_SIZE_MULTICAST
									.name()));
		} else {
			IllegalArgumentException illegalArgumentException = new IllegalArgumentException(
					"Property BUFFER_SIZE_MULTICAST not found");

			logger.error("Property BUFFER_SIZE_MULTICAST no found",
					illegalArgumentException);

			throw illegalArgumentException;
		}

		try {
			multicastManager = new MulticastManagerNetworkLAN(portMulticast,
					InetAddress.getByName(ipMulticast), bufferSize);
		} catch (UnknownHostException e) {
			IllegalArgumentException illegalArgumentException = new IllegalArgumentException(
					"Error of ipmulticast", e);

			logger.error("Error of ipmulticast", illegalArgumentException);

			throw illegalArgumentException;
		}

		return multicastManager;
	}

	/**
	 * Stop messages reciever and multicast manager
	 */
	protected void stop() {
		messagesReciever.stop();
		multicastManager.stop();
	}

}
